package com.tj.collection.controller;

import com.alibaba.fastjson.JSONObject;
import com.tj.collection.aop.anno.VisitorAccessible;
import com.tj.collection.bean.ModuleResponse;
import com.tj.collection.db.dao.ClMerchantDao;
import com.tj.collection.db.entity.ClMerchantEntity;
import com.tj.collection.task.AllocationJob;
import com.tj.collection.task.StageTransferJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @author: moss
 * 2018/10/17
 */
@RequestMapping("/test/185f6751-75a9-4330-8ce8-d9e6ee977aa4")
@RestController
public class TestController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private ClMerchantDao clMerchantDao;

    @Autowired
    private StageTransferJob stageTransferJob;

    @Autowired
    private AllocationJob allocationJob;

    /**
     * @param request
     * @return
     */
    @VisitorAccessible
    @RequestMapping("/test01")
    public ModuleResponse collectionDistributeGet(@RequestBody JSONObject request) {
        List<ClMerchantEntity> entityList = clMerchantDao.selectByParams(new HashMap<>());
        return new ModuleResponse(entityList);
    }

    /**
     * 转单接口
     *
     * @return
     */
    @RequestMapping("/ok/transfer")
    @ResponseBody
    @VisitorAccessible
    public String transfer() {
        stageTransferJob.run();
        return "ok2";
    }

    /**
     * 撤单接口
     *
     * @return
     */
    @RequestMapping("/ok/cancel")
    @ResponseBody
    @VisitorAccessible
    public String transfer2(@RequestBody JSONObject request) {
        String stageCode = request.getString("stageCode");
        int total = stageTransferJob.cancelAllOrders(stageCode);
        return "ok2," + stageCode + ",total=" + total;
    }

    /**
     * 分单接口
     *
     * @return
     */
    @RequestMapping("/ok/allocateV2")
    @ResponseBody
    @VisitorAccessible
    public String allocateV2() {
        try {
            allocationJob.run();
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("分单错误1989", e);
        }
        return "ok-V2";
    }

    /**
     * 上传图片
     *
     * @param request
     * @return
     */
    @VisitorAccessible
    @RequestMapping(value = "/upload", method = RequestMethod.POST, produces = {"application/json;charset=utf-8"})
    public String uploadPicture(HttpServletRequest request) {

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartRequest.getFile("uploadFile");

        if (multipartFile == null || multipartFile.isEmpty()) {
            throw new RuntimeException("666666");
        }

        return "success";
    }

}
